package com.example.campuslink.commen.tools

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity

class PermissionManager(private val activity: FragmentActivity) {

    companion object {
        private val readStoragePermissions =
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
                arrayOf(
                    Manifest.permission.READ_MEDIA_IMAGES,
                    Manifest.permission.READ_MEDIA_VIDEO
                )
            else
                arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)

        private val writeStoragePermissions = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P)
            arrayOf(
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
        else
            emptyArray()

        private val mediaPermissions = arrayOf(
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
        )
    }


    private var onPermissionGranted: (() -> Unit)? = null

    private val requestPermissionLauncher = activity.registerForActivityResult(
        ActivityResultContracts.RequestMultiplePermissions()
    ) { permissions ->
        var allPermissionGranted = true
        permissions.entries.forEach { (permission, granted) ->
            if (!granted) {
                Toast.makeText(
                    activity,
                    "缺少必要权限: $permission",
                    Toast.LENGTH_SHORT
                ).show()
                allPermissionGranted = false
            }
        }
        if (allPermissionGranted) {
            onPermissionGranted?.invoke()
        }
    }


    private fun checkAppPermissions(context: Context, permissions: Array<String>): Boolean {
        for (permission in permissions) {
            if (ContextCompat.checkSelfPermission(
                    context,
                    permission
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                return false
            }
        }
        return true
    }

    fun requestReadStoragePermission(activity: FragmentActivity, onPermissionGranted: () -> Unit) {
        if (!checkAppPermissions(activity, readStoragePermissions)) {
            this.onPermissionGranted = onPermissionGranted
            requestPermissionLauncher.launch(
                readStoragePermissions
            )
        } else {
            onPermissionGranted()
        }
    }

    fun requestPublishPermission(activity: FragmentActivity, onPermissionGranted: () -> Unit) {
        val permissions = readStoragePermissions + writeStoragePermissions + mediaPermissions
        if (!checkAppPermissions(activity, permissions)) {
            this.onPermissionGranted = onPermissionGranted
            requestPermissionLauncher.launch(
                permissions
            )
        } else {
            onPermissionGranted()
        }

    }
}